package com.pronhub.luowowo.website.web.Resolver;

import com.alibaba.dubbo.config.annotation.Reference;
import com.pronhub.luowowo.member.domain.UserInfo;
import com.pronhub.luowowo.service.IUserInfoRedisService;
import com.pronhub.luowowo.website.web.annotation.UserToken;
import com.pronhub.luowowo.website.web.util.CookieUtil;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class UserInfoArgumentResolver implements HandlerMethodArgumentResolver {
    @Override
    public boolean supportsParameter(MethodParameter parameter) {

        return (parameter.getParameterType() ==  UserInfo.class && parameter.hasParameterAnnotation(UserToken.class));
    }

    @Reference
    private IUserInfoRedisService userInfoRedisService;
    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer
            mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {

        HttpServletRequest req = webRequest.getNativeRequest(HttpServletRequest.class);
        HttpServletResponse resp = webRequest.getNativeResponse(HttpServletResponse.class);
        String token = CookieUtil.getToken(req, resp);
        System.err.println(token);
        UserInfo userInfoByToken = userInfoRedisService.getUserInfoByToken(token);
        return userInfoByToken;
    }
}
